package com.zk.algo;

import java.util.*;

/**
 * 树的高度
 *
 * 题目有点坑 [代码暂时还没有通过]
 *
 * https://www.nowcoder.com/practice/4faa2d4849fa4627aa6d32a2e50b5b25?tpId=85&tqId=29897&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2F2017test%2Fquestion-ranking&tPage=4
 *
 * (1) 忘记导入包了
 * (2) 接下来 n - 1 行,不是 n 行
 */
public class ShuDeGaoDu {

    public static void main(String...args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int inputLine = n - 1;

        BinaryTree root = new BinaryTree(0);

        while (inputLine-- > 0) {

            int parentNum = scanner.nextInt();
            int childNum = scanner.nextInt();

            buildTree(root, parentNum, childNum);
        }

        int height = getHeight(root);

        System.out.println(height);

    }

    static int getHeight(BinaryTree root) {
        if (root == null) {
            return 0;
        }

        return 1 + Math.max(getHeight(root.left), getHeight(root.right));
    }

    static void buildTree(BinaryTree root, int parentNum, int childNum) {

        BinaryTree parent = lookup(root, parentNum);
        if (parent.left == null) {
            parent.left = new BinaryTree(childNum);
        } else if (parent.right == null) {
            parent.right = new BinaryTree(childNum);
        }

    }

    static BinaryTree lookup(BinaryTree root, int val) {

        if (root == null) {
            return null;
        }

        if (val == root.num) {
            return root;
        }

        BinaryTree left = lookup(root.left, val);
        if (left != null) {
            return left;
        }

        return lookup(root.right, val);
    }

    static class BinaryTree {

        int num;
        BinaryTree left;
        BinaryTree right;

        public BinaryTree(int num) {
            this.num = num;
        }

        public void setLeft(BinaryTree left) {
            this.left = left;
        }

        public void setRight(BinaryTree right) {
            this.right = right;
        }

        public BinaryTree getLeft() {
            return left;
        }

        public BinaryTree getRight() {
            return right;
        }

    }

}
